using System;
using System.Collections.Generic;
using System.Text;

namespace GraphFS
{


	public sealed class FSNodeCache : Dictionary<Int64, FSNode>
	{



		static readonly FSNodeCache instance = new FSNodeCache();
		static FSNodeCache()
		{
		}
		FSNodeCache()
		{
			Initialize();
		}
		public static FSNodeCache Instance
		{
			get
			{
				return instance;
			}
		}


		private FSNodeList nodeList;
		private void Initialize()
		{
			nodeList = new FSNodeList();
			nodeList.Read("nodelistdata.bin");
			//nodeList.Read();
		}




		public new FSNode this [Int64 i]
		{
			get
			{
				if (!this.ContainsKey(i))
				{
					this.Add(i, nodeList.GetNode(i));
				}
				return this [i];
			}
		}


		List<Int64> items = new List<Int64>();
		public new void Add(long key, FSNode value)
		{
			base.Add(key, value);
			items.Add(key);

			int maxItems = 10000;
			if (items.Count > 2*maxItems)
			{
				//cleanup
				int toRemove = items.Count - maxItems;
				for (int  k = 0; k < toRemove; k++)
				{
					base.Remove(items [k]);
				}
				items.RemoveRange(0, toRemove);

			}
		}


	}




}
